package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.util.ObjectMapperUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("/getMsg")
    public String getMsg(){
        return "单点登录测试";
    }

    @RequestMapping("/user/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param, @PathVariable Integer type,String callback){
        boolean flag=userService.checkUser(param,type);
        SysResult sysresult = SysResult.success(flag);
        return new JSONPObject(callback, sysresult);
    }

    @RequestMapping("/user/findUserList")
    public List<User> findUserList(){
        return userService.selectUserList();

    }

    /**
     * 跨域请求，完成用户信息获取
     */
    @RequestMapping("/user/query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket,String callback){
        //从redis中获取数据
        if(jedisCluster.exists(ticket)){
            String userJSON=jedisCluster.get(ticket);
            SysResult sysResult = SysResult.success(userJSON);
            return new JSONPObject(callback, sysResult);
        }else {
            return new JSONPObject(callback, SysResult.fail());
        }

    }
}
